package com.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.common.ErrorCodeEnum;
import com.model.base.Result;
import com.common.anntation.Log;
import com.common.anntation.SysLog;
import com.common.emums.BusinessType;

import com.common.utils.Utils;
import com.core.convenientCode.ConvenientCodeService;
import com.core.convenientCode.SysGenTableColumnService;
import com.core.convenientCode.SysGenTableService;
import com.core.springSecurity.service.PermissionService;
import com.model.pojo.sys.SysGenTable;
import com.model.pojo.sys.SysGenTableColumn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipOutputStream;

/**
 * @Author: zql
 * @Description: 自动化代码生成接口
 * 包含 Controller的增删改  批量增 批量改
 * 包含 Service层基本的增删改 批量增 批量改
 * @Data: 9:47 2021/6/1
 */
@Api(tags = {"自动代码生成"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class ConvenientCodeController {

    @Resource
    SysGenTableService sysGenTableService;

    @Resource
    SysGenTableColumnService sysGenTableColumnService;

    @Resource
    ConvenientCodeService convenientCodeService;

    @Log(title = "获取数据库中未导入的表" ,businessType = BusinessType.SELECT,isSaveRequestData=true)
    @SysLog()
    @PostMapping("/getNotInsertTable")
    @ApiOperation(value = "获取数据库中未导入的表")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public JSONObject getNotInsertTable(){
        List<SysGenTable> sysGenTableList = sysGenTableService.getNotInsertGenTableList();
        return Utils.result(ErrorCodeEnum.SUCCESS, sysGenTableList);
    }

    @Log(title = "获取表中字段信息" ,businessType = BusinessType.SELECT,isSaveRequestData=true)
    @SysLog()
    @GetMapping("/getTableColumn")
    @ApiOperation(value = "获取表中字段信息")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public JSONObject getNotInsertTable(String tableName){
        List<SysGenTableColumn> sysGenTableColumnList = sysGenTableColumnService.getTableColumnListForDB(tableName);
        return Utils.result(ErrorCodeEnum.SUCCESS, sysGenTableColumnList);
    }


    @SysLog()
    @GetMapping("/syncTable")
    @ApiOperation(value = "同步表")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public JSONObject syncTable(){
        List<String> tableNameListDB =  sysGenTableService.syncGenTable();
        tableNameListDB.forEach(tableName ->  sysGenTableColumnService.syncTableColumn(tableName));
        return Utils.result(ErrorCodeEnum.SUCCESS);
    }

    @SysLog()
    @GetMapping("/syncTableColumn")
    @ApiOperation(value = "同步表字段")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public JSONObject syncTableColumn(String tableName){
        sysGenTableColumnService.syncTableColumn(tableName);
        return Utils.result(ErrorCodeEnum.SUCCESS);
    }


    @GetMapping("/convenientCode")
    @ApiOperation(value = "生成代码")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public void convenientCode(String tableName, HttpServletResponse response) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        convenientCodeService.convenientCodeToZip(tableName,zip);
        IOUtils.closeQuietly(zip);
        genCode(response,outputStream.toByteArray(),tableName);
        //return Result.suc();
    }

    @GetMapping("/convenientCodeToCurrentProject")
    @ApiOperation(value = "生成代码到当前项目")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public Result<Objects> convenientCodeToCurrentProject(String tableName) throws IOException {
        convenientCodeService.convenientCodeToDir(tableName,"./src");
        return Result.suc();
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data,String name) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
       // response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\""+name+".zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream;charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
